﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using UserDemo.Models;

namespace UserDemo.Controllers
{
    /// <summary>
    /// 账户管理
    /// </summary>
    public class AccountController : Controller
    {
        // GET: 登录
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Login(AccountModel model)
        {
            if (ModelState.IsValid)
            {
                var dbHelper = new DbHelper();
                //判断 是否重名
                using (var reader = dbHelper.ExecuteReader("select [Id],[Pwd],[NickName],[Status],[Email] from [User] where LoginName = @n;", new SqlParameter("@n", SqlDbType.NVarChar, 50) { Value = model.Name }))
                {
                    if (reader.Read())
                    {
                        var id = reader.GetInt32(0);
                        var pwd = reader["Pwd"].ToString();
                        if (pwd == model.Pwd)
                        {
                            Session["User"] = new LoginInfo
                            {
                                UserId = id,
                                LoginName = model.Name,
                                NickName = string.Empty,
                                Role = Role.User
                            };
                            return Json(new ResultModel { Msg = "登录成功", Data = id});
                        }

                        //var status = reader.GetInt32(3);
                        //string nickName = reader.GetString(2);
                        //string email = string.Empty;
                        //if (reader["Email"] != DBNull.Value)
                        //{
                        //    email = reader.GetString(4);
                        //}
                    }
                }
                return Json(new ResultModel { Msg = "用户名或密码错误", Code = 2});

            }
            return Json(new ResultModel { Code = 1, Msg = "无效的数据" });
        }



        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        public ActionResult Regist()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Regist(AccountModel model)
        {
            if (ModelState.IsValid)
            {
                var dbHelper = new DbHelper();
                //判断 是否重名
                using (var reader = dbHelper.ExecuteReader("select [Id] from [User] where LoginName = @n;", new SqlParameter("@n", SqlDbType.NVarChar, 50) {Value = model.Name}))
                {
                    if (reader.Read())
                    {
                        //如果 可以对到下一条数据，证明 已有这个用户名
                        return Json(new ResultModel { Code = 2, Msg = "用户已存在" });
                    }
                }
                
                //insert
                var parameters = new SqlParameter[]
                {
                    new SqlParameter("@name", SqlDbType.NVarChar, 50) {Value = model.Name},
                    new SqlParameter("@pwd", SqlDbType.NVarChar, 50) {Value = model.Pwd}
                };
                var reuslt = dbHelper.ExecuteScalar("insert into [user] (LoginName, Pwd, Status) values(@name, @pwd, 0);select scope_identity();", parameters);
                return Json(new ResultModel { Msg = "OK ", Data = reuslt});
            }
            return Json(new ResultModel { Code = 1, Msg = "无效的数据" });
        }
    }
}